﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class admin_Quanlysanpham : System.Web.UI.Page
{
    SanPhamController SanPhamCtr = new SanPhamController();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["Employee"] != null)
            {
                MultiView1.ActiveViewIndex = 0;
                gvdtsanpham.DataSource = SanPhamCtr.funC_GetAllproduct();
                DataBind();
                if (Request["sanphamtk"] == null)
                    gvdtsanpham.DataSource = SanPhamCtr.funC_GetAllproduct();
                else
                {
                    hdtimkiem.Value = Request["sanphamtk"].ToString();
                    string timkiem = "%" + hdtimkiem.Value + "%";
                    gvdtsanpham.DataSource = SanPhamCtr.fnC_TimKiemSanPham(timkiem);
                }
                DataBind();
            }
            else 
            {
                Response.Redirect("login.aspx");
            }

        }
    }
    private void DisplayAddandEdit()
    {
        NhaPPController NhaPPCtr = new NhaPPController();
        ddlNhaPP.DataSource = NhaPPCtr.fnC_GetAllNhaPP();
        NhaSXController NhaSXCTR = new NhaSXController();
        ddlnhasanxuat.DataSource = NhaSXCTR.fnC_GetAllNhaSX();
        DataBind();
        Session["NumricHinh"] = 0;
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {

        SanPhamEntity SanPhamEntity = new SanPhamEntity();
        SanPhamEntity.AnHien = "True";
        SanPhamEntity.DacDiem = ckDacDiem.Text;
        SanPhamEntity.DonGia = 0;
        try{
            SanPhamEntity.MaSP = SanPhamCtr.Taomamoi(SanPhamCtr.fnC_Getmaxproduct().Rows[0][0].ToString(), 2);
        }
        catch
        {
            SanPhamEntity.MaSP = "DP00000001";
        }
        
        SanPhamEntity.HinhAnh = hdimage.Value;
        //fUpload.PostedFile.SaveAs(Server.MapPath("~/images/Product/") + SanPhamEntity.HinhAnh + ".jpg");
        SanPhamEntity.MaDVT = 1;
        SanPhamEntity.MaLoai = 1;
        SanPhamEntity.Mota = ckMoTa.Text;
        SanPhamEntity.NgayNhap = DateTime.Now;
        SanPhamEntity.NhaPhanPhoi = Int32.Parse(ddlNhaPP.SelectedValue);
        if (ckkhac.Checked)
        {
            NhaSXController NhaSXCTR = new NhaSXController();
            NhaSanXuatEntity NhaSXE = new NhaSanXuatEntity();
            NhaSXE.TenNSX = txtnhasanxuat.Text;
            NhaSXCTR.fnC_InsertNSX(NhaSXE);
            SanPhamEntity.NhaSanXuat=NhaSXCTR.fnC_SelectMaxNSX();
        }
        else
            SanPhamEntity.NhaSanXuat = int.Parse(ddlnhasanxuat.SelectedValue);
        SanPhamEntity.SoLuong = 1;
        SanPhamEntity.TenSP = txtTenSP.Text;
        try
        {
           
            SanPhamCtr.fnC_AddNew(SanPhamEntity);
            MultiView1.ActiveViewIndex = 0;
            fillterProduct();

            DisplayAddandEdit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void gvdtsanpham_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvdtsanpham.PageIndex = e.NewPageIndex;
        if (hdtimkiem.Value == "")
        {
            fillterProduct();
        }
        else
        {
            string timkiem = "%" + hdtimkiem.Value + "%";
            gvdtsanpham.DataSource = SanPhamCtr.fnC_TimKiemSanPham(timkiem);
        }
        DataBind();
    }
    protected void gvdtsanpham_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            HiddenField masp = (HiddenField)gvdtsanpham.SelectedRow.Cells[0].FindControl("hdfmasp");
            MultiView1.ActiveViewIndex = 1;
            SanPhamEntity SanPhamE=new SanPhamEntity();
            SanPhamE.MaSP=masp.Value;
            DataTable SanPham = SanPhamCtr.funC_GetDetailProduct(SanPhamE);
            imgSanPham.Src = "../images/product/" + SanPham.Rows[0]["HinhAnh"].ToString()+".jpg";
            txtTenSP.Text = SanPham.Rows[0]["TenSP"].ToString();
            ddlNhaPP.SelectedValue=SanPham.Rows[0]["MaNPP"].ToString();
            ddlnhasanxuat.SelectedValue = SanPham.Rows[0]["MaNSX"].ToString();
            ckDacDiem.Text = SanPham.Rows[0]["DacDiem"].ToString();
            ckMoTa.Text = SanPham.Rows[0]["MoTa"].ToString();
            hdfmasp.Value = SanPham.Rows[0]["MaSP"].ToString();
            hdimage.Value = SanPham.Rows[0]["HinhAnh"].ToString();
            btnEdit.Visible = true;
            btnAddNew.Visible = false;
            lbhead.Text = "Cập Nhật Sản Phẩm";
            DisplayAddandEdit();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void imbtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
        lbhead.Text = "Thêm Mới Sản Phẩm";
        btnAddNew.Visible = true;
        btnEdit.Visible = false;
        txtnhasanxuat.Text = "";
        txtTenSP.Text = "";
        ckDacDiem.Text = "";
        ckMoTa.Text = "";
        imgSanPham.Src = "";
        hdimage.Value = "";
        DisplayAddandEdit();
    }
    protected void gvdtsanpham_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton btn = (ImageButton)e.Row.Cells[6].Controls[0];
            //khi click vao nut xoa se phat sinh doan ma javascript thong bao xac nhan co muon xoa hay khong
            btn.OnClientClick = "if(!confirm('Bạn có muốn xóa sản phẩm :" + Server.HtmlDecode(e.Row.Cells[1].Text) + "  không?'))return false";
        }
    }
    protected void gvdtsanpham_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SanPhamEntity SanPhamE = new SanPhamEntity();
        HiddenField masp = (HiddenField)gvdtsanpham.Rows[e.RowIndex].Cells[0].FindControl("hdfmasp");
        Image imagehinhsp = (Image)gvdtsanpham.Rows[e.RowIndex].Cells[2].FindControl("Image1");
        SanPhamE.MaSP = masp.Value;
        SanPhamCtr.DeleteSp(SanPhamE);
        File.Delete(Server.MapPath(imagehinhsp.ImageUrl));
        gvdtsanpham.DataSource = SanPhamCtr.funC_GetAllproduct();
        DataBind();
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        SanPhamEntity SanPhamEntity = new SanPhamEntity();
        SanPhamEntity.AnHien = "True";
        SanPhamEntity.DacDiem = ckDacDiem.Text;
        SanPhamEntity.DonGia = 0;
        SanPhamEntity.MaSP = hdfmasp.Value;
        SanPhamEntity.HinhAnh = hdimage.Value;

        SanPhamEntity.MaDVT = 1;
        SanPhamEntity.MaLoai = 1;
        SanPhamEntity.Mota = ckMoTa.Text;
        SanPhamEntity.NgayNhap = DateTime.Now;
        SanPhamEntity.NhaPhanPhoi = Int32.Parse(ddlNhaPP.SelectedValue);
        if (ckkhac.Checked)
        {
            NhaSXController NhaSXCTR = new NhaSXController();
            NhaSanXuatEntity NhaSXE = new NhaSanXuatEntity();
            NhaSXE.TenNSX = txtnhasanxuat.Text;
            NhaSXCTR.fnC_InsertNSX(NhaSXE);
            SanPhamEntity.NhaSanXuat = NhaSXCTR.fnC_SelectMaxNSX();
        }
        else
            SanPhamEntity.NhaSanXuat = int.Parse(ddlnhasanxuat.SelectedValue);
        SanPhamEntity.SoLuong = 1;
        SanPhamEntity.TenSP = txtTenSP.Text;
        try
        {

            SanPhamCtr.fnC_UpdateProduct(SanPhamEntity);

            MultiView1.ActiveViewIndex = 0;
            fillterProduct();

            DisplayAddandEdit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Session["NumricHinh"] = 0;
        Response.Redirect("Quanlysanpham.aspx");
    }

    protected void ddlnhaphanphoi_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillterProduct();
    }
    private void fillterProduct()
    {
        if (ddlnhaphanphoi.SelectedIndex == 0)
        {
            gvdtsanpham.DataSource = SanPhamCtr.funC_GetAllproduct();
        }
        else if (ddlnhaphanphoi.SelectedIndex == 1)
        {
            gvdtsanpham.DataSource = SanPhamCtr.fnC_GetProductByNhaPP("DaiPhat");
        }
        else
        {
            gvdtsanpham.DataSource = SanPhamCtr.fnC_GetProductByNhaPP("VietHan");
        }
        DataBind();
    }
}